package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {



    //@Bean Bingding Queue Exchange

    /**
     * 容器中的 Bingding Queue Exchange都会自动创建(RabbitMQ)没有的情况
     * RabbitMQ只要有.@Bean声明属性发生变化也不会覆盖
     * @return
     */
    //队列1
    @Bean
    public Queue orderDelayQueue(){
        //构造
        Map<String,Object> arguments = new HashMap<>();

        //放入参数
        //死性路由
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        //死性路由键
        arguments.put("x-dead-letter-routing-key","order.release.order");
        //消息过期时间
        arguments.put("x-message-ttl",60000);
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        //创建队列
        Queue queue = new Queue("order.delay.queue", true, false, false, arguments);
        //返回队列
        return queue;
    }
    //队列2
    @Bean
    public Queue orderReleaseOrderQueue(){
        //创建普通队列
        Queue queue = new Queue("order.release.order.queue", true, false, false);
        //返回队列
        return queue;
    }
    //交换机1
    @Bean
    public Exchange orderEventExchange(){
        //创建返回topic交换机
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("order-event-exchange",true,false);
    }
    //绑定1
    @Bean
    public Binding orderCreateOrderBinding(){
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //			Map<String, Object> arguments
        //创建绑定关系队列1
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);

    }
    //绑定2
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOrtherBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }
}
